package org.rqb.utils;

import com.alibaba.fastjson2.JSON;
import org.rqb.entry.Point;

import java.util.HashMap;

import static org.rqb.utils.CodeUtil.base64Encode;

public class ProxyConversionUtil {

    /**
     * 添加trojan节点转化
     * trojan://<password>@<server>:<port>?security=tls&sni=<sni>&udp=<true/false>#<name>
     *  trojan://er668@kfjdi.pages.dev:443?security=tls&sni=kfjdi.pages.dev&type=ws&host=kfjdi.pages.dev&path=%2F%3Fed%3D2560#%E7%BE%8E%E5%9B%BD%28yudou66.com%20%E7%8E%89%E8%B1%86%E5%85%8D%E8%B4%B9%E8%8A%82%E7%82%B9%29
     * @param point
     * @return
     */
    public static String trojanToV2Str(Point point) {
        String trojanStr = "trojan://" + point.getPassword()
                + "@" + point.getServer()
                + ":" + point.getPort()
                + "?security=" + point.getTls()
                + "&sni=" + point.getSni()
                + "&type=" + point.getType()
//                + "&host=" + point.getHost()
//                + "&path=" + urlEncode(point.getPath())
                + "#" + point.getName();

        return trojanStr;
    }

    /**
     * 拼接vless节点信息
     * @param point
     * @return
     */
    public static String vlessToV2Str(Point point) {



        return "";
    }

    /**
     * 根据point 节点信息加密节点
     * @param point
     *   "v": "2",
     *   "ps": "自定义名称",
     *   "add": "11.mamamajd.site",
     *   "port": "23611",
     *   "id": "975eba47-5d04-3761-aca6-4bfee48b8fe9",
     *   "aid": "2",
     *   "scy": "auto",
     *   "net": "ws",
     *   "type": "none",
     *   "host": "11.mamamajd.site",
     *   "path": "/",
     *   "tls": ""
     * @return
     */
    public static String vmessToV2Str(Point point) {
        HashMap<String, String> map = new HashMap<>();
        map.put("v","2");
        map.put("ps",point.getName());
        map.put("add",point.getServer());
        map.put("port",String.valueOf(point.getPort()));
        map.put("id",point.getUuid());
        map.put("aid",String.valueOf(point.getAlterId()));
        map.put("scy",point.getCipher());
        map.put("net",point.getNetwork());
        map.put("type",point.getType());
        map.put("host",point.getHost());
        map.put("path",point.getPath());
        map.put("tls",point.getTls().toString());

        String json = JSON.toJSONString(map);

        String vmess = "vmess://" +  base64Encode(json);

        return vmess;
    }

    /**
     * 根据point 节点信息获取加密节点
     * @param point
     * @return
     */
    public static String ssToV2Str(Point point) {
        // 1. 生成 "加密方式:密码@服务器:端口"
        String password = point.getCipher() + ":" + point.getPassword();

        // 2. Base64 编码服务器信息
        String passwordBase64 = base64Encode(password);


        // 3. 拼接成完整的 ss:// 链接
        String ss = "ss://" + passwordBase64
                + "@" + point.getServer()
                + ":" + point.getPort()
                + "?plugin=v2ray-plugin"
                + ";mode=" + point.getMode()        // http / websocket
                + ";host=" + point.getHost()        // 伪装域名
                + ";path=" + point.getPath()        // 如果没有 path，可以传 "/"
                + "#" + point.getName();
        return ss;

    }


    /**
     * 封装ss协议
     * @param pointStr
     * @return
     */
    public static Point ssToYmlStr(String pointStr) {
        Point point = new Point();
        String[] part = pointStr.split("://|@|:|#");
        point.setType(part[0]);
        String cipherAndPasswordStr = CodeUtil.base64Decode(part[1]);
        String[] cipherAndPasswordArr = cipherAndPasswordStr.split(":");
        point.setCipher(cipherAndPasswordArr[0]);
        point.setPassword(cipherAndPasswordArr[1]);
        point.setServer(part[2]);
        point.setPort(Integer.parseInt(part[3]));
        point.setName(CodeUtil.urlDecode(part[4]));
        return point;
    }


    public static Point vmessToYmlStr(String pointStr) {
        return null;
    }

    public static Point vlessToYmlStr(String pointStr) {
        Point point = new Point();


        return point;
    }

    public static Point trojanToYmlStr(String pointStr) {
        Point point = new Point();



        return point;
    }
}
